<?php

class Base_Controller extends Controller {

	/**
	 * Catch-all method for requests that can't be matched.
	 *
	 * @param  string    $method
	 * @param  array     $parameters
	 * @return Response
	 */

	public function __construct()
    {
        //Assets
        // Asset::add('jquery', 'ultraslim/javascripts/jquery.js');
        // Asset::add('jquery', 'js/jquery-1.7.2.min.js');
        // Asset::add('modernizr-js', 'ultraslim/javascripts/modernizr.foundation.js');
        // Asset::add('foundation-js', 'ultraslim/javascripts/foundation.min.js');
        // Asset::add('app-js', 'ultraslim/javascripts/app.js');
        // Asset::add('easySlider-js', 'js/easySlider1.7.js');
        //Asset::add('bootstrap-css', 'css/bootstrap.min.css');
        //Asset::add('bootstrap-css-responsive', 'css/bootstrap-responsive.min.css', 'bootstrap-css');

        // Asset::add('foundation-css', 'ultraslim/stylesheets/foundation.css');
        // Asset::add('main-css', 'ultraslim/stylesheets/main.css');
        // Asset::add('app-css', 'ultraslim/stylesheets/app.css');
        // Asset::add('slider-css', 'css/screen.css');
        parent::__construct();

        Bundle::start('datagrid');

        //setup navigation
        $xml = simplexml_load_file(path('public')."/navigation/navigation.xml");
        
        foreach($xml->children() as $child)
          {
            if($child->controller->name == URI::segment(1)):
                $child->status = "active";
            endif;
            
            //set for homepage
            if(in_array($child->controller->name,array('home','blog','gallery','about'))):
                if(Auth::check()):
                    $child->auth = 0;
                endif;
            endif; 

            foreach($child->page->action as $linkPage){
            //set for internal page
                if(in_array($linkPage->name,array('menu','system'))):
                    $linkPage->auth = 1;
                else:
                    $linkPage->auth = 0;
                endif;
            }

        }

        // $selected = array();
        // $submenu = array();

        // foreach ($Menus as $id => $menu)
        // {
        //     if($menu->action == NULL){

        //         if($menu->controller == URI::segment(1)):
        //             $selected['content'][$id]['status'] = "active";
        //         endif;

        //         $selected['content'][$id]['footer'] = $menu->footer;
        //         $selected['content'][$id]['header'] = $menu->header;
        //         $selected['content'][$id]['auth'] = $menu->auth;
        //         $selected['content'][$id]['controller']['name'] = $menu->controller;
        //         $selected['content'][$id]['controller']['alias'] = $menu->controlleralias;

        //     }else{

        //         $submenu[$menu->controller]['action'][] = array(
        //             'name' => $menu->action, 
        //             'alias' => $menu->actionalias,
        //             'footer' => $menu->footer,
        //             'header' => $menu->header,
        //             'auth' => $menu->auth
        //         );

        //     }
        // }

        // foreach ($selected['content'] as $key => $value) {
            
        //     $ctrl = $value['controller']['name'];

        //     if(!empty($submenu[$ctrl])){

        //          $selected['content'][$key]['page'] = $submenu[$ctrl];
        //     }          

        // }



        View::share('navLinks', Menu::menuGenerator());


        //Filters
        $class = get_called_class();
        switch($class) {
            case 'Home_Controller':
                $this->filter('before', 'nonauth');
                break;
            
            case 'Auth_Controller':
                $this->filter('before', 'auth|acl')->except(array('index','authenticate','register','signup','logout'));
                break;
             
            case 'Admin_Controller':
                if(Auth::check() && Auth::user()->role == 1){
                    $this->filter('before', 'auth|acl')->except(array('acl'));
                }else{
                    $this->filter('before', 'auth|acl');
                }
                break;

            case 'User_Controller':
                $this->filter('before', 'auth|acl')->except(array('profile'));     

            case 'Dashboard_Controller':
                $this->filter('before', 'auth|acl')->except(array('index'));
                break;   
                
            default:
                $this->filter('before', 'auth');
                $this->filter('before', 'acl');
                break;
        }


     }

	public function __call($method, $parameters)
	{
		return Response::error('404');
	}

}